<template>
  <div id="add">
    <!-- 头部 -->
    <div class="navebar">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="人员编号">
          <el-input
            v-model="formInline.taskCode"
            placeholder="请输入"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">
            查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格新建按钮 -->
    <div class="boxs">
      <div class="btn">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          class="btn1"
          @click="addClick"
        >新建</el-button>
      </div>
      <!-- 表格 -->
      <div class="tabel bos">
        <el-table
          v-loading="loading"
          stripe
          tooltip-effect="dark"
          :data="SearchList"
          style="width: 100%"
          :header-cell-style="{
            //标题头部的颜色
            background: '#f5f7fa',
            height: '45px',
          }"
        >
          <el-table-column fixed type="index" label="序号" width="90" />
          <el-table-column prop="userName" label="人员名称" width="220" />
          <el-table-column prop="regionName" label="归属区域" width="230" />
          <el-table-column prop="vmRow" label="角色" width="230">
            <template slot-scope="scope">
              {{ role[scope.row.roleId] }}
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="联系电话" width="230" />
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="medium "
                @click="cargo(scope.row)"
              >修改</el-button>
              <el-button
                type="text"
                size="medium "
                style="color: red"
                @click="delgo(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 实现分页
        current-page 当前页数
        page-size 每页显示个数
        total 总条目数
        current-change 页面改变时会触发
        prev-text="上一页"
        next-text="下一页"
         -->
        <div class="block">
          <el-pagination
            background
            :current-page.sync="pageOption.pageIndex"
            :page-size="pageOption.pagesize"
            layout="total,  pager, prev, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 新建弹出框 -->
    <div class="dils">
      <el-dialog
        :title="title"
        :visible.sync="centerDialogVisible"
        width="53%"
        :before-close="closeFn"
        @open="openFn"
      >
        <!-- 表单内容 -->
        <el-form
          ref="departRef"
          :model="departForm"
          :rules="departRules"
          label-width="120px"
        >
          <el-form-item label="人员名称" prop="userName">
            <el-input
              v-model="departForm.userName"
              style="width: 85%"
              placeholder="请输入"
              maxlength="15"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="角色" prop="roleId">
            <el-select
              v-model="departForm.roleId"
              style="width: 85%"
              placeholder="请选择"
            >
              <el-option
                v-for="item in roleIdList"
                :key="item.id"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话" prop="mobile">
            <el-input
              v-model="departForm.mobile"
              style="width: 85%"
              placeholder="请输入"
              maxlength="11"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="负责区域" prop="regionId">
            <el-select
              v-model="departForm.regionId"
              style="width: 85%"
              placeholder="请选择"
              @change="selectChanged"
            >
              <el-option
                v-for="item in regionNameList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备图片" prop="image">
            <!-- 图片上传组件 -->
            <UploadImg v-model="departForm.image" />
            <div style="font-size: 14px;color: #bac0cd; margin-top: -7px;">支持扩展名：jpg、png，文件不得大于100kb</div>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-switch v-model="departForm.status" />
          </el-form-item></el-form>
        <!-- 底部按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button class="btnquxiao" @click="closeFn">取 消</el-button>
          <el-button
            type="primary"
            class="btn1"
            @click="submitWork(departForm)"
          >确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getUserApi, serveList, getRegionListApi, addServeList } from '@/api'
import UploadImg from '@/components/UploadImg'
import { Message } from 'element-ui'
export default {
  name: 'PeopleMemt',
  components: {
    UploadImg
  },
  data() {
    return {
      loading: false,
      deltacticsVisible: false,
      centerDialogVisible: false,
      SearchList: [], // 表格
      currentPage1: 10,
      total: 0,
      imageUrl: '',
      roleIdList: [], // 角色下拉
      regionNameList: [],
      // 搜索框
      formInline: {
        taskCode: ''
      },
      // 搜索传入的数值
      pageOption: {
        pageIndex: 1,
        pagesize: 10
      },
      // 售货机状态枚举
      vm: {
        0: '未投放',
        1: '运营',
        3: '撤机'
      },
      // 角色枚举
      role: {
        2: '运营员',
        3: '运维员'
      },
      num: '',
      // 新增点位数据
      departForm: {
        userName: '', // 人员名称
        roleId: '', // 角色
        mobile: '', // 联系电话
        regionId: '', // 负责区域
        regionName: '',
        image: '', // 图片上传
        status: false // 状态
      },
      // 表单校验
      departRules: {
        userName: [
          { required: true, message: '名称不能为空', trigger: ['blur'] }
        ],
        roleId: [
          { required: true, message: '请选择', trigger: ['blur'] }
        ],
        mobile: [
          { required: true, message: '请填写', trigger: ['blur'] }
        ],
        regionName: [
          { required: true, message: '请选择', trigger: ['blur'] }
        ],
        image: [
          { required: true, message: '请上传头像', trigger: ['blur'] }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.SearchList.length > 1 ? '新增人员' : '编辑人员'
    }
  },
  created() {
    this.searchDevice()
  },
  methods: {
    // 封装页面初始化请求数据
    async searchDevice() {
      const { data } = await getUserApi(this.pageOption)
      console.log(data)
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
      this.loading = false
    },
    // 封装弹窗打开请求的数据
    async  openCenterDialog() {
      const { data } = await serveList()
      this.roleIdList = data
      const res = await getRegionListApi({
        pageIndex: 1,
        pageSize: 100000
      })
      console.log(res)
      this.regionNameList = res.data.currentPageRecords
    },

    // 查询
    async onSubmit() {
      console.log('submit!')
      this.loading = true
      const { data } = await getUserApi({
        ...this.pageOption,
        userName: this.formInline.taskCode
      })
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
      this.loading = false
    },

    // 弹窗打开执行的回调
    openFn() {
      this.openCenterDialog()
    },

    // 新建
    async addClick() {
      this.centerDialogVisible = true
      this.openCenterDialog()
    },

    // 获取下拉框的label值
    selectChanged(id) {
      console.log(id)
      this.departForm.regionName = this.regionNameList.find(item => item.id === id).name
      console.log(this.departForm)
    },

    // 点击确定
    // 请求接口
    // 添加人员
    // 提示用户
    // 刷新页面
    async submitWork() {
      console.log('确定')
      await addServeList(this.departForm)
      Message.success('新增人员成功')
      this.searchDevice()
    },

    // 修改
    // 点击修改弹窗开启
    // 传入点击行的数据，赋值给表单绑定的变量
    // 实现数据回显
    cargo(val) {
      console.log('修改')
      this.centerDialogVisible = true
      console.log(val)
      this.departForm = val
    },

    // 删除
    delgo() {},

    // 关闭弹窗
    closeFn() {
      this.centerDialogVisible = false
      this.departForm = {
        name: '',
        model: '',
        vmRow: '',
        vmCol: '',
        channelMaxCapacity: ''
      }
    },

    // 实现分页
    handleCurrentChange(val) {
      this.loading = true
      console.log(val)
      this.pageOption.pageIndex = val
      this.getDeviceList()
      this.loading = false
    }
  }
}
</script>

<style lang="scss">
#add {
  overflow: hidden;
  background-color: #f8fafd;
}
.navebar {
  margin: 20px 19px 0;
  padding-top: 14px;
  padding-left: 18px;
  height: 64px;
  background-color: #fff;
}
// 表格
.boxs {
  margin: 20px 19px 0;
  padding: 17px 0 1px 18px;
  background-color: #fff;
}
// 按钮
.btn1 {
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: #f8fafd;
  height: 35px;
}
.btn2 {
  background-color: #fbf4f0;
  border: #f8fafd;
  color: #f97138;
  height: 35px;
}
// 表格高度
.el-table .el-table__cell {
  padding: 3px 0 0 0;
}
// 图片
.imgphoto {
  display: block;
  width: 24px;
  height: 24px;
  -o-object-fit: cover;
  object-fit: cover;
  border-radius: 50%;
  background: #f3f6fb;
  border: 1px solid #f3f6fb;
}
// 内容
.el-descriptions {
  padding: 0px 0px 0px 30px;
}
.dils .el-dialog__footer {
  margin-top: -8px;
  padding: 10px 248px 20px;
}
.dils .el-dialog__body {
  padding: 30px 46px;
}
.dils .el-descriptions-item__label .has-colon {
  margin-right: 19px;
}
.dialog-footer {
  margin-top: 10px;
}
.btnquxiao {
  margin-right: 20px;
  height: 37px;
}
.button {
  background-color: #fbf4f0;
  border: #f8fafd;
  color: #f97138;
}
// 分页器
.block {
  margin: 20px 0;
}
.tabel {
  margin-top: 19px;
}
.btn-prev {
  margin-left: 100px;
}
// 图片上传
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
</style>
